package com.syni.mdd.yhd.common.all.listen.comment;

import com.syni.mdd.yhd.common.all.constants.AuroraPushCode;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.entity.*;
import com.syni.mdd.yhd.common.all.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
 * @className OrderListener
 * @description TOO
 * @Author cfx
 * @DATE 2019/8/8 15:46
 * @VERSION 1.0
 **/
@Component
public class AnswerToAnswerListener implements ApplicationListener<CommentEvent> {
    private final static String MODEL = "answerToAnswer";

    @Autowired
    BmsUserVideoService bmsUserVideoService;

    @Autowired
    BmsDxNewsService bmsDxNewsService;

    @Autowired
    BmsBusinessService bmsBusinessService;

    @Autowired
    BmsDxClientRoleService bmsDxClientRoleService;

    @Override
    @Async
    public void onApplicationEvent(CommentEvent commentEvent) {
        String model = commentEvent.getSource().toString();
        if(!MODEL.equals(model)){
            return;
        }
        BmsVideo bmsVideo = commentEvent.getBmsVideo();
        BmsDxAnswer bmsDxAnswer = commentEvent.getBmsDxAnswer();
        BmsDxAnswer replyAnswer = commentEvent.getReplyAnswer();
        //回复To回复
        if(bmsDxAnswer.getBmsUserId().intValue() == replyAnswer.getBmsUserId().intValue()){
            if(bmsDxAnswer.getFromOrigin().intValue() == replyAnswer.getFromOrigin().intValue()){
                return;
            }
        }
        BmsDxNews bmsDxNews = new BmsDxNews();
        bmsDxNews.setIsDelete(Constants.COMMON_FLAG_FALSE_INT);
        Set<Integer> roleIds = new HashSet<>();
        if(replyAnswer.getFromOrigin() == Constants.USER_CLIENT){
            roleIds.add(Constants.GENERAL_USER_ROLE_ID);
        }else if(replyAnswer.getFromOrigin() == Constants.BUSINESS_CLIENT){
            roleIds.add(Constants.BUSINESS_ROLE_ID);
            roleIds.add(Constants.ASSISTANT_ROLE_ID);
        }
        List<BmsDxClientRole> bmsDxClientRoles = bmsDxClientRoleService.getBaseDao().findByBmsUserIdAndRoleIdIn(replyAnswer.getBmsUserId(),roleIds);
        if(!(bmsDxClientRoles != null && bmsDxClientRoles.size() > 0)){
            return;
        }
        bmsDxNews.setOriginRole(bmsDxClientRoles.get(0).getRoleId());
        bmsDxNews.setChildType(BmsDxNewsService.PARENTTYPT_INT_ANSWER);
        bmsDxNews.setBmsUserId(replyAnswer.getBmsUserId());
        bmsDxNews.setBmsBusinessId(replyAnswer.getBmsBusinessId());
        bmsDxNews.setOpUserId(bmsDxAnswer.getBmsUserId());
        bmsDxNews.setBmsVideoId(bmsVideo.getId());
        bmsDxNews.setBmsCommentId(replyAnswer.getBmsDxCommentId());
        bmsDxNews.setBmsAnswerId(bmsDxAnswer.getId());
        bmsDxNews.setParentType(BmsDxNewsService.PARENTTYPT_INT);
        bmsDxNews.setContent(bmsDxAnswer.getContent());
        if(bmsDxAnswer.getFromOrigin() == Constants.USER_CLIENT) {
            bmsDxNews.setTitile(bmsDxNewsService.getBmsNewsTitle(bmsDxAnswer.getBmsUserId()));
        }else {
            BmsBusiness business = bmsBusinessService.getBaseDao().findById(bmsDxAnswer.getBmsBusinessId()).get();
            bmsDxNews.setTitile(business.getVendorName());
        }
        bmsDxNewsService.saveNews(bmsDxNews,bmsVideo);
        String alert = String.format(AuroraPushCode.pushTemplateMap.get(AuroraPushCode.USER_DO_ANSWER_ANSWER).getAlert(),bmsDxNews.getTitile());
        BmsCommonService.pushToFront(bmsDxNews,AuroraPushCode.USER_DO_ANSWER_ANSWER,alert);
    }
}
